package com.showcai.controller.prizes;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.Activity;
import com.showcai.module.Organization;
import com.showcai.module.Prizes;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ActivityService;
import com.showcai.service.OrganizationService;
import com.showcai.service.PrizesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-6.
 */
@RequestMapping("/prizes")
@Controller
public class PrizesController extends BaseController {

    @Autowired
    PrizesService prizesService;
    @Autowired
    ActivityService activityService;

    @RequestMapping(value = "/getPrizesList")
    public ModelAndView getPrizesList() {
        Map<String,String> map = super.showRequestParameters();
        ModelAndView mov = new ModelAndView();
        PageInfo pageInfo = prizesService.getPrizesList(map);
        mov.addObject("pageInfo",pageInfo);
        mov.setViewName("/prizes/prizesList");
        return mov;
    }
    @RequestMapping(value = "/prizes")
    public ModelAndView prizes(Long tid) {
        ModelAndView mov = new ModelAndView();
        Prizes prizes = prizesService.selectPrizesByTid(tid);
        List<Activity> activityList = activityService.getActivityListByType(Activity.ACTIVITY_TYPE_DRAW);
        mov.addObject("prizes",prizes);
        mov.addObject("activityList",activityList);
        mov.setViewName("/prizes/prizes");
        return mov;
    }
    @RequestMapping(value = "/modifyPrizes")
    @ResponseBody
    public String modifyOrganization(Prizes prizes,@RequestParam(value = "file", required = false) MultipartFile file) {
        ResultObj resultObj = new ResultObj();
        String picUrl = "";
        String filePath = "showcai/prizes";
        try {
            if (file != null) {
                try {
                    picUrl = super.putObject(file.getInputStream(), filePath, file.getOriginalFilename());
                    logger.info("picUrl=============>>>" + picUrl);
                    prizes.setPicUrl(picUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        prizesService.modifyPrizes(prizes);
        return resultObj.toJson();
    }


}
